package com.tianqingwl.gsystem.tags.processors;

import com.tianqingwl.gsystem.resources.pojo.SysConfig;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import com.tianqingwl.gsystem.tags.GSystemDialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IAttribute;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.Map;

/**
 * 系统配置标签，通过group属性取出对应配置键值组
 */
@Component
public class SysConfigTagProcessor extends AbstractElementTagProcessor {
    @Autowired
    private SysConfigService sysConfigService;


    private static final String TAG_NAME="sysConfig";
    private static final int PRECEDENCE = 10000;

    public SysConfigTagProcessor() {
        super(TemplateMode.HTML, GSystemDialect.PREFIX, TAG_NAME, true, null, false, PRECEDENCE);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {
        IAttribute group = tag.getAttribute("group");

        if (group!=null){
            Map mapByGroup = sysConfigService.getMapByGroup(group.getValue());
            structureHandler.setLocalVariable(group.getValue(),mapByGroup);
        }
        structureHandler.removeTags();
    }
}
